//
//  SZLMykeepViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/18.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLMykeepViewController.h"
#import <MJRefresh.h>
#import "ShoppingMallsCell.h"
#import "ConGoodsDetailController.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "ClubCareCell.h"
#import <UIImageView+WebCache.h>
#import "ConClubShowViewController.h"
#import "ConNewClubDetailVc.h"

@interface SZLMykeepViewController()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic, strong) NSMutableArray *typeArray;
@property (nonatomic,assign) NSInteger curPage;

@end

@implementation SZLMykeepViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"我的关注";
    self.curPage = 1;
    [self.view addSubview:self.sourceTab];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getGoodsList];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NormalShopGoodsModel *model = self.sources[indexPath.row];
    if ([model.type isEqualToString:@"1"]) {
        NSLog(@"🌹走到了商品这里");
        ShoppingMallsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ShoppingMallsCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.sources[indexPath.row];
        return cell;
    }else {
        NSLog(@"🌹走到了俱乐部这里");
        
        ClubCareCell *clubCell = [tableView dequeueReusableCellWithIdentifier:@"ClubCareCell"];
        clubCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        clubCell.clubIntroductionLabel.text = model.name;
        
        [clubCell.clubLogoImageview sd_setImageWithURL:[NSURL URLWithString:model.thumbnailUrl] placeholderImage:GetImage(@"beijngtu")];
        return clubCell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NormalShopGoodsModel *model = self.sources[indexPath.row];
    if ([model.type isEqualToString:@"1"]) {
        ConGoodsDetailController *goodsVc = [[ConGoodsDetailController alloc]init];
        goodsVc.productNo = model.productNo;
        __WEAK
        goodsVc.cancelBlock = ^(BOOL isCancel){
            self.curPage = 1;
            [weakSelf getGoodsList];
        };
        [self.navigationController pushViewController:goodsVc animated:YES];
    }else {
        ConNewClubDetailVc *detailVc = [[ConNewClubDetailVc alloc]init];
        detailVc.ID = [model.productNo integerValue];
        [self.navigationController pushViewController:detailVc animated:YES];
    }
}
- (void)getGoodsList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [PersonNetWork getMyListWithParame:parame type:self.type response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.sourceTab.mj_footer endRefreshing];
        if (success) {
            [self hideRequestAgainView];
            [self hideNoDataView];
            self.sourceTab.mj_footer.hidden = (lists.count<20)?YES:NO;
            if (self.curPage==1) {
                [self.sources removeAllObjects];
                if (lists.count == 0) {
                    [self showNoDataView:self.sourceTab.frame title:@"暂无关注内容"];
                }
            }else{
                if (lists.count == 0) {
                    self.curPage--;
                    return;
                }
            }
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getGoodsList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        _sourceTab.dataSource = self;
        _sourceTab.delegate = self;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([ShoppingMallsCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([ShoppingMallsCell class])];
        [_sourceTab registerClass:[ClubCareCell class] forCellReuseIdentifier:@"ClubCareCell"];
        _sourceTab.rowHeight = 120;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        _sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getGoodsList];
        }];
    }
    return _sourceTab;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}
@end
